package flc.ast.dialog;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.colorpicker.lib.ColorPicker;
import com.stark.mindmap.lib.core.model.DefNodeData;

import flc.ast.R;
import flc.ast.databinding.DialogMdmEditNodeBinding;
import stark.common.basic.base.BaseEventDialog;
import stark.common.basic.utils.Str2NumUtil;

/**
 * Author：daizhongbin on 2023/8/14
 * Email：1027154338@qq.com
 * Desc: 添加编辑节点对话框
 */
public class MyMindAddEditDialog extends BaseEventDialog<DialogMdmEditNodeBinding> {

    private static final int TYPE_ADD = 1;
    private static final int TYPE_EDIT = 2;
    private boolean isAddSubNode;

    private int type;
    private DefNodeData nodeData;

    private MyMindAddEditDialog(@NonNull Activity activity) {
        super(activity);
    }

    public static MyMindAddEditDialog createAddDialog(@NonNull Activity activity, boolean isAddSubNode) {
        MyMindAddEditDialog dialog = new MyMindAddEditDialog(activity);
        dialog.type = TYPE_ADD;
        dialog.isAddSubNode = isAddSubNode;
        return dialog;
    }

    public static MyMindAddEditDialog createEditDialog(@NonNull Activity activity) {
        MyMindAddEditDialog dialog = new MyMindAddEditDialog(activity);
        dialog.type = TYPE_EDIT;
        return dialog;
    }

    public void setNodeData(DefNodeData nodeData) {
        this.nodeData = nodeData;
        if (mContentDataBinding != null) {
            updateViewsContent();
        }
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.dialog_mdm_edit_node;
    }

    @Override
    protected void initContentView(View view) {
        if (type == TYPE_ADD) {
            if (isAddSubNode) {
                mContentDataBinding.tvTitle.setText(getContext().getString(R.string.mdm_add_son_node));
            } else {
                mContentDataBinding.tvTitle.setText(getContext().getString(R.string.mdm_add_peer_node));
            }
        } else {
            mContentDataBinding.tvTitle.setText(getContext().getString(R.string.mdm_edit_node));
        }
        mContentDataBinding.tvTextColor.setOnClickListener(v -> {
            showColorPicker(false);
        });
        mContentDataBinding.tvBgColor.setOnClickListener(v -> {
            showColorPicker(true);
        });
        mContentDataBinding.ivCancel.setOnClickListener(v -> dismiss());
        mContentDataBinding.ivConfirm.setOnClickListener(v -> {
            handleClickConfirm();
        });
        mContentDataBinding.ivDelNode.setVisibility(type == TYPE_ADD ? View.GONE : View.VISIBLE);
        mContentDataBinding.ivDelNode.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDelNode();
            }
            dismiss();
        });
        updateViewsContent();
    }

    private void updateViewsContent() {
        if (nodeData == null) return;
        mContentDataBinding.etText.setText(nodeData.text);
        mContentDataBinding.etTextSize.setText(String.valueOf(nodeData.textSize));
        mContentDataBinding.tvTextColor.setBackgroundColor(nodeData.textColor);
        mContentDataBinding.tvBgColor.setBackgroundColor(nodeData.bgColor);
    }

    private void handleClickConfirm() {
        if (listener == null) return;

        //text
        String text = mContentDataBinding.etText.getText().toString().trim();
        if (TextUtils.isEmpty(text)) {
            ToastUtils.showShort(com.stark.common.res.R.string.please_input_value);
            mContentDataBinding.etText.requestFocus();
            return;
        }
        nodeData.text = text;
        //textSize
        text = mContentDataBinding.etTextSize.getText().toString().trim();
        if (TextUtils.isEmpty(text)) {
            ToastUtils.showShort(com.stark.common.res.R.string.please_input_value);
            mContentDataBinding.etTextSize.requestFocus();
            return;
        }
        final int MIN_TEXT_SIZE = 6;
        int textSize = (int) Str2NumUtil.parse(text, MIN_TEXT_SIZE);
        if (textSize < MIN_TEXT_SIZE) {
            ToastUtils.showShort(getContext().getString(R.string.mdm_min_text_size_tip, MIN_TEXT_SIZE));
            mContentDataBinding.etTextSize.requestFocus();
            return;
        }
        nodeData.textSize = textSize;
        listener.onConfirm(nodeData);
        dismiss();
    }

    private void showColorPicker(boolean isShowBgColor) {
        ColorPicker colorPicker = new ColorPicker(getContext());
        colorPicker.setListener(pickedColor -> {
            if (isShowBgColor) {
                nodeData.bgColor = pickedColor;
                mContentDataBinding.tvBgColor.setBackgroundColor(pickedColor);
            } else {
                nodeData.textColor = pickedColor;
                mContentDataBinding.tvTextColor.setBackgroundColor(pickedColor);
            }
        });
        colorPicker.setColor(isShowBgColor ? nodeData.bgColor : nodeData.textColor);
        colorPicker.show();
    }

    private IListener listener;

    public void setListener(IListener listener) {
        this.listener = listener;
    }

    public interface IListener {

        void onConfirm(@NonNull DefNodeData nodeData);

        void onDelNode();

    }
}
